"""
autor:zhangtao
gongneng:学习工具集
"""
import tkinter as tk
import tkinter.messagebox
import webbrowser
import datetime
from winsound import Beep
import threading
import sys
import time
from PIL import ImageTk, Image
import pygame
import biaoqian
from biaoqian import biaoqian
from jieping import jiepinggongneng


class Xuexigongjuji:
    """
    此类是主页面的绘制和计时功能实现
    """
    def __init__(self):
        self.count = 0
        self.thread_flag = True
        self.music_flag = True
        self.tataltime = 0
        self.yemian()

    def mymsg(self):
        """
            提示完成第一个番茄钟
        """
        try:
           tk.messagebox.showinfo("提示", "恭喜完成一个学习计时！！记得休息一下")
        except Exception as e_flag:
            print(type( e_flag),  e_flag)
            sys.exit()

    def mymsg2(self):
        '''
        提示休息完毕
        '''
        self.root.messagebox.showinfo("提示", "休息完毕！")

    def jisiclock(self, remain_time):
        """
        提示完成第一个番茄钟
        """
        # 如果在休息时间未结束就开启番茄钟，则停止音乐
        pygame.mixer.music.pause()
        # 避免未进行番茄钟时长选择
        if remain_time == 0:
            self.lb3.configure(text='请先选择计时时长')
            return
        print(remain_time)
        begin_time = time.strftime('%H:%M:%S', time.gmtime(remain_time))
        self.lb2.configure(text=begin_time)
        self.lb3.configure(text='总时间/剩余时间')
        # global thread_flag
        if self.thread_flag:
            self.thread_flag = False
        else:
            self.thread_flag = True
        tmp_thread_flag = self.thread_flag
        for i in range(remain_time):
            if tmp_thread_flag != self.thread_flag:
                return
            remain_time -= 1
            self.tataltime += 1
            tataltime_str = time.strftime('%H:%M:%S', time.gmtime(self.tataltime))
            self.lb7.configure(text=tataltime_str)
            remain_time_str = time.strftime('/ %H:%M:%S', time.gmtime(remain_time))
            self.lb1.configure(text=remain_time_str)
            self.root.update()
            time.sleep(1)
            if remain_time == 0:
                Beep(500, 800)
                self.jishi_count()
                self.mymsg()
        self.lb1.configure(text=begin_time)
        self.relax()

    def jishi_count(self):
        """
        完成的番茄钟的数目
        """
        # global count
        self.count += 1
        self.lb4.configure(text=self.count)

    def relax(self):
        """
        xiuxikongzhi
        """
        remain_time = 480  # 休息8分钟
        begin_time = time.strftime('%H:%M:%S', time.gmtime(remain_time))
        self.lb2.configure(text=begin_time)
        self.lb3.configure(text='总时间/剩余时间')

        # 线程标志，用于结束旧线程
        # global thread_flag
        if self.thread_flag:
            self.thread_flag = False
        else:
            self.thread_flag = True
        tmp_thread_flag = self.thread_flag
        pygame.mixer.music.play(-1)
        for i in range(remain_time):
            if tmp_thread_flag != self.thread_flag:
                return
            remain_time -= 1
            remain_time_str = time.strftime('/ %H:%M:%S', time.gmtime(remain_time))
            self.lb1.configure(text=remain_time_str)
            self.root.update()
            time.sleep(1)
            if remain_time == 0:
                pygame.mixer.music.pause()
                self.mymsg2()
        self.lb1.configure(text=begin_time)

    def music_allow(self):
        """
        yingyuekongzhi
        """
         # 如果已经是True(即不禁止音乐时)，勾选了按钮，则music_flag 变为 False，禁止音乐
        if self.music_flag:
            self.music_flag = False
            pygame.mixer.music.set_volume(0.0)
        else:
            self.music_flag = True
            pygame.mixer.music.set_volume(0.5)

    def chinesemooc(self):
        """
        zhongguomooc
        """
        url = 'https://www.icourse163.org/'
        webbrowser.open(url)

    def youdaocidian(self):
        """
                youdaocidian
        """
        url = 'https://www.youdao.com/'
        webbrowser.open(url)

    def bilibili(self):
        """
        bilibili
        """
        url = 'https://www.bilibili.com/'
        webbrowser.open(url)

    def labelshezhi(self):
        """
        yemiansehji
        """
        var = tk.IntVar()
        # 给变量赋初值为0
        var.set(0)

        # 番茄动态计时
        self.lb1 = tk.Label(self.root, text='0', bg='white',
                            fg='red', font='Verdana 16 bold', width=14, height=1)
        self.lb1.place(x=340, y=230)

        # 番茄固定时间
        self.lb2 = tk.Label(self.root, text='0', bg='white',
                            fg='black', font='Verdana 16 bold', width=8, height=1)
        self.lb2.place(x=250, y=230)

        # 剩余时间/总时间
        self.lb3 = tk.Label(self.root, text=' ', bg='white',
                            fg='black', font='Verdana 16 bold', width=20, height=2)
        self.lb3.place(x=250, y=165)

        # 计时个数显示
        self.lb4 = tk.Label(self.root, text='0', bg='white',
                            fg='black', font='Verdana 16 bold', width=10, height=1)
        self.lb4.place(x=90, y=130)

        # 计时：
        lb5 = tk.Label(self.root, text='已积累计时数：', bg='White',
                         fg='black', font='Verdana 16 bold', width=12,height=1)
        lb5.place(x=10, y=130)
        #积累的学习时间
        lb6 = tk.Label(self.root, text='总学习时间：', bg='White',
                       fg='black', font='Verdana 16 bold', width=12, height=1)
        lb6.place(x=10, y=550)
        #总学习时间
        self.lb7 = tk.Label(self.root, text='0', bg='white',
                            fg='black', font='Verdana 16 bold', width=10, height=1)
        self.lb7.place(x=150, y=550)

        # 按钮
        ##创造一个frame来收纳按钮
        fr1 = tk.LabelFrame(self.root, bg='white', text='选择计时时长', relief='groove', bd=0, )
        fr1.place(x=680, y=200)
        radio_1 = tk.Radiobutton(fr1, text='20min', variable=var, bg='white', value=1200)
        radio_1.pack()
        radio_2 = tk.Radiobutton(fr1, text='40min', variable=var, bg='white', value=2400)
        radio_2.pack()
        radio_3 = tk.Radiobutton(fr1, text='50min', variable=var, bg='white', value=3000)
        radio_3.pack()
        radio_4 = tk.Radiobutton(fr1, text='研究生入学考试', variable=var, bg='white', value=10800)
        radio_4.pack()
        radio_5 = tk.Radiobutton(fr1, text='CET', variable=var, bg='white', value=7800)
        radio_5.pack()
        radio_6 = tk.Radiobutton(fr1, text='大学考试', variable=var, bg='white', value=7200)
        radio_6.pack()
        checkbutton = tk.Checkbutton(fr1, text="是否禁止音乐", fg='black', bg='white',
                                     command=self.music_allow)
        checkbutton.pack()

        button1 = tk.Button(self.root, text='开始计时', bg='orange', fg='black', font='Verdana 13 bold',
                            width=10,height=1,command=lambda: threading.Thread(target=
                self.jisiclock(var.get()),daemon=True,args=(var.get(),)).start())
        button1.place(x=200, y=300)

        # 休息一下
        button2 = tk.Button(self.root, text='休息一下', bg='cornflowerblue', fg='black',
                font='Verdana 13 bold',width=10, height=1,
                command=lambda: threading.Thread(target=self.relax, daemon=True).start())
        button2.place(x=200, y=350)
        # bainqian
        button3 = tk.Button(self.root, text='便签', bg='cornflowerblue', fg='black',
                  font='Verdana 13 bold',width=10, height=1,
                  command=lambda: threading.Thread(target=biaoqian(), daemon=True).start())
        button3.place(x=200, y=400)

        button4 = tk.Button(self.root, text='截屏', bg='cornflowerblue', fg='black',
                font='Verdana 13 bold',width=10, height=1,
                command=lambda: threading.Thread(target=jiepinggongneng(), daemon=True).start())
        button4.place(x=200, y=450)

        button5 = tk.Button(self.root, text='打开中国mooc', bg='cornflowerblue', fg='black',
                            font='Verdana 13 bold',
                            width=10, height=1,
                            command=lambda:
                            threading.Thread(target=self.chinesemooc(), daemon=True).start())
        button5.place(x=450, y=300)

        button6 = tk.Button(self.root, text='有道词典', bg='cornflowerblue', fg='black',
                            font='Verdana 13 bold',
                            width=10, height=1,
                            command=lambda:
                            threading.Thread(target=self.youdaocidian(), daemon=True).start())
        button6.place(x=450, y=350)

        button7 = tk.Button(self.root, text='bilibili', bg='cornflowerblue', fg='black',
                            font='Verdana 13 bold',
                            width=10, height=1,
                            command=lambda:
                            threading.Thread(target=self.bilibili(), daemon=True).start())
        button7.place(x=450, y=400)
        # 添加按钮，以及按钮的文本，并通过command 参数设置关闭窗口的功能
        button = tk.Button(self.root, text="退出", fg='black', bg='YellowGreen',
                           width=17,height=1, command=self.root.quit)
        # 将按钮放置在主窗口内
        button.place(x=450, y=450)

    def beijing(self):
        """
        背景图片
        """
        self.canvas = tk.Canvas(self.root, width=800, height=600)
        imageduque = Image.open('beijin.png').resize((200, 204))
        self.im_root = ImageTk.PhotoImage(imageduque)
        self.canvas.create_image(400, 300, image=self.im_root)
        self.canvas.pack()
        lb10 = tk.Label(self.root, text='欢迎使用本学习工具集', bg='white', fg='black',
                        font='Verdana 16 bold',
                        width=16, height=1)
        lb10.place(x=280, y=20)

    def daojishi(self):
        """
                倒计时
        """
        yantime = datetime.datetime(2023, 12, 24)
        now = datetime.datetime.now()
        lctime = time.localtime()
        lctimee = time.strftime("%Y-%m-%d", lctime)

        label2 = tk.Label(self.root, text="今天是" + str(lctimee), font=("仿宋", 18), fg="black")
        label2.place(x=300, y=60)

        label3 = tk.Label(self.root, text="距离研究生招生考试还有" + str((yantime - now).days+1) + "天",
                          font=("华文琥珀", 20),
                          fg="green")
        label3.place(x=250, y=100)

    def yemian(self):
        """
        绘制页面
        :return:完整的页面
        """
        self.root = tk.Tk()
        # 给主窗口起一个名字，也就是窗口的名字
        self.root.title('学习工具集')
        # 设置窗口大小:宽x高,
        self.root.geometry('800x600')
        self.root.configure(bg='white')
        pygame.mixer.init()
        # 异常抛出，防止没有放音乐文件
        try:
            pygame.mixer.music.load(r'C:\Users\27740\Music\Diavid Hoffner - Positive Outlook.mp3')
        except Exception as exchange:
            print(type(exchange), exchange)
            tk.messagebox.showinfo("提示", "无文件music.mp3或改文件路径不对")
            sys.exit()
        pygame.mixer.music.set_volume(0.5)
        self.beijing()
        self.daojishi()
        self.labelshezhi()

        self.root.mainloop()

a = Xuexigongjuji()
